package io.renren.modules.dynamic.service.impl;

import io.renren.common.exception.RRException;
import io.renren.commons.dynamic.datasource.annotation.TransactionDataSource;
import io.renren.commons.dynamic.datasource.config.DynamicConnectionHolder;
import io.renren.modules.dynamic.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * 数据处理
 *
 * @author hou 2019-5-29 10:12:03
 */
@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Override
    @TransactionDataSource(value = "slave1")
    public void delFirst() throws SQLException {
        Connection connection = DynamicConnectionHolder.peek();
        connection.setAutoCommit(false);
        String sql = "delete from sys_user where user_id = 2";
        PreparedStatement ppst = connection.prepareStatement(sql);
        int i = ppst.executeUpdate();
        log.info("del first carry i:{}", i);
    }

    @Override
    @TransactionDataSource(value = "slave2")
    public void delThirdRuntime() throws SQLException {
        Connection connection = DynamicConnectionHolder.peek();
        connection.setAutoCommit(false);
        String sql = "delete from sys_user where user_id = 2";
        PreparedStatement ppst = connection.prepareStatement(sql);
        int i = ppst.executeUpdate();
        log.info("del third runtime carry i:{}", i);
        throw new RRException("del third runtime ex");
    }

    @Override
    @TransactionDataSource(value = "slave2")
    public void thirdThrowEx() {

    }

    @Override
    @TransactionDataSource(value = "slave2")
    public void delThirdThrowEx() {

    }

    @Override
    @TransactionDataSource(value = "slave2")
    public void delThirdSuccess() {

    }
}
